package com.kzw.oa.action.system;

import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.kzw.core.command.QueryFilter;
import com.kzw.core.util.BeanUtil;
import com.kzw.core.web.action.BaseAction;
import com.kzw.oa.model.system.AppUser;
import com.kzw.oa.model.system.Department;
import com.kzw.oa.model.system.UserOrg;
import com.kzw.oa.service.ext.AppUserService2;
import com.kzw.oa.service.ext.DepartmentService2;
import com.kzw.oa.service.system.AppUserService;
import com.kzw.oa.service.system.UserOrgService;

public class UserOrgAction extends BaseAction {
	@Resource
	private AppUserService appUserService;
	@Resource
	private DepartmentService2 deptService;
	@Resource
	private AppUserService2 appUserService2;
	@Resource
	private UserOrgService userOrgService;
	private UserOrg userOrg;

	private Long userOrgId;

	public Long getUserOrgId() {
		return userOrgId;
	}

	public void setUserOrgId(Long userOrgId) {
		this.userOrgId = userOrgId;
	}

	public UserOrg getUserOrg() {
		return userOrg;
	}

	public void setUserOrg(UserOrg userOrg) {
		this.userOrg = userOrg;
	}
	
	/**
	 * 查找某一用户所有的部门列表
	 * 
	 * @return
	 */
	public String find() {

		String userId = getRequest().getParameter("userId");
		if (StringUtils.isNotEmpty(userId)) {
			List<UserOrg> userOrgs = userOrgService
					.getDepOrgsByUserId(new Long(userId));
			StringBuffer sb = new StringBuffer("{success:true,result:[");
			for (UserOrg userOrg : userOrgs) {
				sb.append("{userOrgId:'").append(userOrg.getUserOrgId())
						.append("',orgId:'")
						.append(userOrg.getDept().getDepId())
						.append("',orgName:'");
				Department org = userOrg.getDept();
				sb.append(org.getDepName());
				sb.append("',isPrimary:'").append(userOrg.getIsPrimary());
				Short isCharge = userOrg.getIsCharge() == null ? 0 : userOrg
						.getIsCharge();
				sb.append("',isCharge:'").append(isCharge);
				sb.append("'},");
			}
			if (userOrgs.size() > 0) {
				sb.deleteCharAt(sb.length() - 1);
			}
			sb.append("]}");
			jsonString = sb.toString();
			return SUCCESS;
		}
		// 自动选中
		String orgId = getRequest().getParameter("depId");
		if (StringUtils.isNotEmpty(orgId)) {
			Department org = deptService.get(Long.parseLong(orgId));
			StringBuffer sb = new StringBuffer("{success:true,result:[{");
			sb.append("orgId:").append(orgId);
			sb.append(",orgName:'").append(org.getDepName());
			sb.append("',isPrimary:1,isCharge:0}]}");
			jsonString = sb.toString();
			return SUCCESS;
		}
		jsonString = "{success:true,result[]}";
		return SUCCESS;
	}

	/**
	 * 为某个部分添加所属组织或部门
	 * 
	 * @return
	 */
	public String addOrgs() {
		String userIds = getRequest().getParameter("userIds");
		String orgId = getRequest().getParameter("depId");
		
		if (StringUtils.isNotEmpty(userIds)) {
			String[] uIds = userIds.split("[,]");
			Department org = deptService.get(new Long(orgId));
			for (String id : uIds) {
				List<UserOrg> userOrgs = userOrgService.getByUserIdOrgId(new Long(id),
						new Long(orgId));
				if (userOrgs.size() == 0) {
					UserOrg userOrg = new UserOrg();
					AppUser user = appUserService.get(new Long(id));
					userOrg.setAppUser(user);
					userOrg.setDept(org);
					userOrg.setIsPrimary(UserOrg.PRIMARY);
					userOrg.setIsCharge(UserOrg.NOT_CHARGE);
					userOrgService.save(userOrg);
				}
			}
		}
		return SUCCESS;
	}

	/**
	 * 显示列表
	 */
	public String list() {

		QueryFilter filter = new QueryFilter(getRequest());
		List<UserOrg> list = userOrgService.getAll(filter);

		Type type = new TypeToken<List<UserOrg>>() {
		}.getType();
		StringBuffer buff = new StringBuffer("{success:true,'totalCounts':")
				.append(filter.getPagingBean().getTotalItems()).append(
						",result:");

		Gson gson = new Gson();
		buff.append(gson.toJson(list, type));
		buff.append("}");

		jsonString = buff.toString();

		return SUCCESS;
	}

	/**
	 * 批量删除
	 * 
	 * @return
	 */
	public String multiDel() {

		String userIds = getRequest().getParameter("ids");
		String orgId = getRequest().getParameter("depId");
		for (String uid : userIds.split(",")) {
			if (uid.length() > 0) {
				userOrgService.delCascade(new Long(uid), new Long(orgId));
			}
		}
		jsonString = "{success:true}";

		return SUCCESS;
	}

	/**
	 * 显示详细信息
	 * 
	 * @return
	 */
	public String get() {
		UserOrg userOrg = userOrgService.get(userOrgId);

		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
		// 将数据转成JSON格式
		StringBuffer sb = new StringBuffer("{success:true,data:");
		sb.append(gson.toJson(userOrg));
		sb.append("}");
		setJsonString(sb.toString());

		return SUCCESS;
	}

	/**
	 * 添加及保存操作
	 */
	public String save() {
		if (userOrg.getUserOrgId() == null) {
			userOrgService.save(userOrg);
		} else {
			UserOrg orgUserOrg = userOrgService.get(userOrg.getUserOrgId());
			try {
				BeanUtil.copyNotNullProperties(orgUserOrg, userOrg);
				userOrgService.save(orgUserOrg);
			} catch (Exception ex) {
				logger.error(ex.getMessage());
			}
		}
		setJsonString("{success:true}");
		return SUCCESS;

	}

	/**
	 * 删除对应的用户组织
	 * 
	 * @return
	 */
	public String del() {
		if (userOrgId != null) {
			userOrgService.remove(userOrgId);
		}
		return SUCCESS;
	}
}
